package com.accounting.juanjuan.utils;

import cn.hutool.core.date.DateUtil;
import com.accounting.juanjuan.exception.AppException;
import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 列表工具类，多用于转换
 *
 * @Author xxj
 * @Date 2024/1/28 20:34
 * @Version 1.0
 */
public class ListUtil {
    /**
     * 边成string列表，防止解析异常
     */
    public static List<List<String>> objectList2StringList(List<List<Object>> objects) {
        List<List<String>> listList = new ArrayList<>();
        for (List<Object> objs : objects) {
            List<String> stringList = new ArrayList<>();
            for (Object obj : objs) {
                if (null == obj) {
                    stringList.add("");
                } else if (obj instanceof Number) {
                    stringList.add(String.valueOf(obj));
                } else if (obj instanceof Date) {
                    Date date = (Date) obj;
                    if (date.getHours() == 0 && date.getMinutes() == 0 && date.getSeconds() == 0) {
                        stringList.add(DateUtil.format((Date) obj, "yyyy/MM/dd"));
                    } else {
                        stringList.add(DateUtil.format((Date) obj, "yyyy-MM-dd HH:mm:ss"));
                    }
                } else {
                    stringList.add(obj.toString());
                }
            }
            listList.add(stringList);
        }
        return listList;
    }

    /**
     * string转列表,jsonArray 好像是只能转字符串形式的，对于其他类型数据会报错
     */
    public static List<List<String>> stringList(String data) {
        try {
            JSONArray jsonArray = new JSONArray(data);
            List<List<String>> resultList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONArray innerArray = jsonArray.getJSONArray(i);
                List<String> innerList = new ArrayList<>();
                for (int j = 0; j < innerArray.length(); j++) {
                    innerList.add(innerArray.getString(j));
                }
                resultList.add(innerList);
            }
            return resultList;
        } catch (JSONException e) {
            throw new AppException("数据解析错误！");
        }
    }

    /**
     * string列表转为带有金额（double）的列表，要生成文件利于求和
     */
    public static List<List<Object>> stringList2MoreType(List<List<String>> lists, String columnName) {
        List<List<Object>> listList = new ArrayList<>();
        List<String> stringList = lists.get(0);
        int columnIndex = stringList.indexOf(columnName);
        if (-1 == columnIndex) {
            throw new AppException("系统异常：未获得列下标！");
        }
        List<Object> objs = new ArrayList<>(stringList);
        listList.add(objs);
        //标题单独添加,循环从第二行开始
        for (int j = 1; j < lists.size(); j++) {
            List<Object> objects = new ArrayList<>();
            for (int i = 0; i < lists.get(j).size(); i++) {
                if (i == columnIndex) {
                    objects.add(Double.valueOf(lists.get(j).get(i)));
                } else {
                    objects.add(lists.get(j).get(i));
                }
            }
            listList.add(objects);
        }
        return listList;
    }
}
